﻿/*
Libraries Needed To Link Against For This program.
Add to Linker Settings In Whichever Development
Environment You Are Using.  If A Microsoft Product
The Libraries Won't Start With The Prefix lib.

..\..\..\Program Files\CodeBlocks\MinGW\lib\libodbccp32.a
..\..\..\Program Files\CodeBlocks\MinGW\lib\libodbc32.a

*/
#include      <windows.h>
#include      <tchar.h>
#include      <stdio.h>          //iostream can eat s*** and die
#include      <string.h>
#include      <sql.h>            //ODBC header file
#include      <sqlext.h>         //ODBC header file
#include      "Strings.h"
#include      "Sql.h"            //Sql Class used to offer ODBC functionality.
#include      "SqlProcs.h"       //Some of my ODBC wrappers to hide some real grungy ODBC code
#include	  <iostream>

using namespace std;

int GetExcelRecordCount(SQL& Sql)
{
	unsigned int iRecCt=0;
	TCHAR szQuery[128];
	SQLHSTMT hStmt;
	long iJnk;

	_tcscpy(szQuery,_T("SELECT Count(*)  As RecordCount From [Sheet1$];"));
	SQLAllocHandle(SQL_HANDLE_STMT,Sql.hConn,&hStmt);
	SQLBindCol(hStmt,1,SQL_C_ULONG,&iRecCt,0,&iJnk);
	if(SQLExecDirect(hStmt,(SQLTCHAR*)szQuery,SQL_NTS)!=SQL_SUCCESS)
	{
		SQLGetDiagRec(SQL_HANDLE_STMT,hStmt,1,Sql.szErrCode,&Sql.iNativeErrPtr,Sql.szErrMsg,512,&Sql.iTextLenPtr);
		SQLFreeHandle(SQL_HANDLE_STMT,hStmt);
		return -1;
	}
	else
	{
		SQLFetch(hStmt);
		SQLCloseCursor(hStmt);
		SQLFreeHandle(SQL_HANDLE_STMT,hStmt);
		return iRecCt;
	}
}

void blnDumpExcelData( SQL& sql, char* sheetName ) //default 10 language
{
	SQLHSTMT hStmt;
	char     szQuery[256];
	long	iJnk;
	char	textID[50];
	char	vaule[7][1024];
	char sqlStr[256];

	//get sql string
	strcpy ( sqlStr, "SELECT ID, EN, FR, DE, ES, IT, BR, VN FROM [");
	strcat ( sqlStr, sheetName );
	strcat ( sqlStr, "$];" );

	if( SQLAllocHandle ( SQL_HANDLE_STMT,sql.hConn,&hStmt ) == SQL_SUCCESS )
	{
		_tcscpy ( szQuery,  sqlStr );

		SQLBindCol( hStmt, 1, SQL_C_TCHAR, textID, sizeof ( textID ), &iJnk );  
		SQLBindCol( hStmt, 2, SQL_C_TCHAR, vaule[0], sizeof ( vaule[0] ), &iJnk );  
		SQLBindCol( hStmt, 3, SQL_C_TCHAR, vaule[1], sizeof ( vaule[1] ), &iJnk );  
		SQLBindCol( hStmt, 4, SQL_C_TCHAR, vaule[2], sizeof ( vaule[2] ), &iJnk );  
		SQLBindCol( hStmt, 5, SQL_C_TCHAR, vaule[3], sizeof ( vaule[3] ), &iJnk );  
		SQLBindCol( hStmt, 6, SQL_C_TCHAR, vaule[4], sizeof ( vaule[4] ), &iJnk );  
		SQLBindCol( hStmt, 7, SQL_C_TCHAR, vaule[5], sizeof ( vaule[5] ), &iJnk );  
		SQLBindCol( hStmt, 8, SQL_C_TCHAR, vaule[6], sizeof ( vaule[6] ), &iJnk );  
    
		if( SQLExecDirect( hStmt, ( SQLTCHAR* ) szQuery, SQL_NTS ) == SQL_SUCCESS )
		{
			while ( SQLFetch( hStmt ) != SQL_NO_DATA )
			{   
				if ( strcmp ( textID, "" ) != 0 )
				printf ( "\n%-20s %-20s %-20s %-20s %-20s %-20s %-20s %-20s", textID, vaule[0], vaule[1], vaule[2], vaule[3], vaule[4],vaule[5] ,vaule[6] );			
				memset ( textID , 0, sizeof ( textID ) );
				memset ( vaule , 0, sizeof ( vaule ) );
			}
       
		}
		SQLCloseCursor( hStmt );
		SQLFreeHandle( SQL_HANDLE_STMT, hStmt );   
	}

}

void BuildArrayTextEachSheet( SQL& sql, char* sheetName, char* textClass ) //default 10 language
{
	SQLHSTMT hStmt;
	char     szQuery[256];
	long	iJnk;
	char	textID[50];
	char	vaule[7][1024];
	char sqlStr[256];

	FILE* outF = fopen ( textClass, "a" );

	//get sql string
	strcpy ( sqlStr, "SELECT ID, EN, FR, DE, ES, IT, BR, VN FROM [");
	strcat ( sqlStr, sheetName );
	strcat ( sqlStr, "$];" );
	memset ( textID , 0, sizeof ( textID ) );
	memset ( vaule , 0, sizeof ( vaule ) );
	if( SQLAllocHandle ( SQL_HANDLE_STMT,sql.hConn,&hStmt ) == SQL_SUCCESS )
	{
		_tcscpy ( szQuery,  sqlStr );

		SQLBindCol( hStmt, 1, SQL_C_TCHAR, textID, sizeof ( textID ), &iJnk );  
		SQLBindCol( hStmt, 2, SQL_C_TCHAR, vaule[0], sizeof ( vaule[0] ), &iJnk );  
		SQLBindCol( hStmt, 3, SQL_C_TCHAR, vaule[1], sizeof ( vaule[1] ), &iJnk );  
		SQLBindCol( hStmt, 4, SQL_C_TCHAR, vaule[2], sizeof ( vaule[2] ), &iJnk );  
		SQLBindCol( hStmt, 5, SQL_C_TCHAR, vaule[3], sizeof ( vaule[3] ), &iJnk );  
		SQLBindCol( hStmt, 6, SQL_C_TCHAR, vaule[4], sizeof ( vaule[4] ), &iJnk );  
		SQLBindCol( hStmt, 7, SQL_C_TCHAR, vaule[5], sizeof ( vaule[5] ), &iJnk );  
		SQLBindCol( hStmt, 8, SQL_C_TCHAR, vaule[6], sizeof ( vaule[6] ), &iJnk );  
    
		if( SQLExecDirect( hStmt, ( SQLTCHAR* ) szQuery, SQL_NTS ) == SQL_SUCCESS )
		{
			while ( SQLFetch( hStmt ) != SQL_NO_DATA )
			{   
				if ( strcmp ( textID, "" ) != 0 )
				{
					fprintf ( outF, "\n\t{ \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\" },", vaule[0], vaule[1], vaule[2], vaule[3], vaule[4], vaule[5], vaule[6] );
				}
				memset ( textID , 0, sizeof ( textID ) );
				memset ( vaule , 0, sizeof ( vaule ) );
			}
       
		}
		SQLCloseCursor( hStmt );
		SQLFreeHandle( SQL_HANDLE_STMT, hStmt );   
	}

	fclose ( outF );
}

void BuildArrayText (SQL& sql, char* textClassCPP)
{
	FILE* outF = fopen ( textClassCPP, "w" );
	
	fprintf ( outF, "#include \"Text.h\"" );
	fprintf ( outF, "\n#include \"Language.h\"" );

	fprintf ( outF, "\n\nchar s_text[TEXT::TEXT_MAX][LANGUAGE::MAX][256] = {" );
	fclose ( outF );

	BuildArrayTextEachSheet ( sql, "Menu", textClassCPP );
	BuildArrayTextEachSheet ( sql, "InGame", textClassCPP );

	outF = fopen ( textClassCPP, "a" );
	fprintf ( outF, "\n};" );
	fclose ( outF );


}

//return last textID
int BuildClassEachSheet ( SQL& sql, char* sheetName, char* textClass, int countTextID_start )
{
	SQLHSTMT hStmt;
	char     szQuery[256];
	long	iJnk;
	char	textID[50];	
	char sqlStr[256];
	int		countTextID = countTextID_start;
	//char* sheetNameUp = strupr ( sheetName );
	FILE* outF = fopen ( textClass, "a" );

	//get sql string
	strcpy ( sqlStr, "SELECT ID, EN, FR, DE, ES, IT, BR, VN FROM [");
	strcat ( sqlStr, sheetName );
	strcat ( sqlStr, "$];" );

	if( SQLAllocHandle ( SQL_HANDLE_STMT,sql.hConn,&hStmt ) == SQL_SUCCESS )
	{
		_tcscpy ( szQuery,  sqlStr );

		SQLBindCol( hStmt, 1, SQL_C_TCHAR, textID, sizeof ( textID ), &iJnk );  		
		if( SQLExecDirect( hStmt, ( SQLTCHAR* ) szQuery, SQL_NTS ) == SQL_SUCCESS )
		{
			char* upcaseSheetName = new char[ strlen ( sheetName ) ];
			strcpy ( upcaseSheetName, sheetName );
			while ( SQLFetch( hStmt ) != SQL_NO_DATA )
			{   
				if ( strcmp ( textID, "" ) != 0 )
					fprintf ( outF, "\n\tstatic const int %s_%s = %d;",  strupr ( upcaseSheetName ) , strupr( textID ), countTextID++ );
				memset ( textID , 0, sizeof ( textID ) );				
			}
       
		}
		SQLCloseCursor( hStmt );
		SQLFreeHandle( SQL_HANDLE_STMT, hStmt );   
	}

	fclose ( outF );

	return countTextID;
}

void BuildClass ( SQL& sql, char* textClass )
{
	int countTextID_start = 0;
	FILE* outF = fopen ( textClass, "w" );

	//write header
	fprintf ( outF, "#ifndef Text_h" );
	fprintf ( outF, "\n#define Text_h" );
	fprintf ( outF, "\n\nclass TEXT\n{\npublic:" );
	fclose ( outF );

	//write TextID
	countTextID_start = BuildClassEachSheet ( sql, "Menu", textClass, countTextID_start );
	countTextID_start = BuildClassEachSheet ( sql, "Ingame", textClass, countTextID_start );

	//write end class
	outF = fopen ( textClass, "a" );
	fprintf ( outF, "\n\tstatic const int TEXT_MAX = %d;", countTextID_start );	
	fprintf ( outF, "\n\n};\n\n#endif" );	
	fclose ( outF );
}

int BuildGtsFileEachSheet ( SQL& sql, char* sheetName, char* gtsFile, int countTextID_start )
{
	SQLHSTMT hStmt;
	char     szQuery[256];
	long	iJnk;
	char	textID[50];	
	char sqlStr[256];
	int		countTextID = countTextID_start;
	//char* sheetNameUp = strupr ( sheetName );
	FILE* outF = fopen ( gtsFile, "a" );

	//get sql string
	strcpy ( sqlStr, "SELECT ID, EN, FR, DE, ES, IT, BR, VN FROM [");
	strcat ( sqlStr, sheetName );
	strcat ( sqlStr, "$];" );

	if( SQLAllocHandle ( SQL_HANDLE_STMT,sql.hConn,&hStmt ) == SQL_SUCCESS )
	{
		_tcscpy ( szQuery,  sqlStr );

		SQLBindCol( hStmt, 1, SQL_C_TCHAR, textID, sizeof ( textID ), &iJnk );  		
		if( SQLExecDirect( hStmt, ( SQLTCHAR* ) szQuery, SQL_NTS ) == SQL_SUCCESS )
		{
			char* upcaseSheetName = new char[ strlen ( sheetName ) ];
			strcpy ( upcaseSheetName, sheetName );
			while ( SQLFetch( hStmt ) != SQL_NO_DATA )
			{   
				if ( strcmp ( textID, "" ) != 0 )
					fprintf ( outF, "\n\t %d = \"%s_%s\"", countTextID++, strupr ( upcaseSheetName ) , strupr( textID ) );
				memset ( textID , 0, sizeof ( textID ) );				
			}
       
		}
		SQLCloseCursor( hStmt );
		SQLFreeHandle( SQL_HANDLE_STMT, hStmt );   
	}

	fclose ( outF );

	return countTextID;
}

void BuildGtsFile ( SQL& sql, char* gtsFile )
{
	FILE* outF = fopen ( gtsFile, "w" );
	fclose ( outF );
	int countTextID_start = 0;
	countTextID_start = BuildGtsFileEachSheet ( sql, "Menu", gtsFile, countTextID_start );
	countTextID_start = BuildGtsFileEachSheet ( sql, "Ingame", gtsFile, countTextID_start );
}


void BuildText ( char* textName )
{
	TCHAR lpBuffer[MAX_PATH];
	DWORD nBufLen=MAX_PATH;
	int iRecCt=0;
	SQL Sql;

	Sql.strDriver=_T ( "Microsoft Excel Driver (*.xls)" );
	GetCurrentDirectory (nBufLen, lpBuffer );
	Sql.strDBQ = lpBuffer;

	Sql.strDBQ = Sql.strDBQ + "\\"  +  textName;
	
	Sql.ODBCConnect();
	
	if( Sql.blnConnected == true )
	{			
		BuildClass( Sql, "Text.h" );			
		BuildArrayText( Sql, "Text.cpp" );
		BuildGtsFile (  Sql, "text.gts" );
		Sql.ODBCDisconnect();
	}
	else
		printf("\nCan not build Text %s!", textName);
}

void main ( int argv, char* arg[] )
{	
	//BuildText ( "text.xls" );
	BuildText ( arg[1] );
	cout << endl;
	//system("pause");
 
}
